package com.kg.elysia.controller;

import com.kg.elysia.service.Neo4jService;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

/**
 * <p>
 *
 * </p>
 *
 * @author zj
 * @since 2025/10/20 17:30
 */
public class Neo4jController {

    private final Vertx vertx;

    private final Neo4jService neo4jService;

    public Neo4jController(Vertx vertx) {
        this.vertx = vertx;
        this.neo4jService = new Neo4jService(vertx);
    }


    public void getFullGraph(RoutingContext ctx) {
        neo4jService.getFullGraph()
                .onSuccess(result -> ctx.response()
                        .putHeader("Content-Type", "application/json")
                        .end(result.encode()))
                .onFailure(err -> sendError(ctx, 500, err.getMessage()));
    }

    private void sendError(RoutingContext ctx, int code, String message) {
        ctx.response()
                .setStatusCode(code)
                .end(new JsonObject().put("error", message).encode());
    }

    public void close() {
        neo4jService.close();
    }
}
